unit uFuncoes;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sqldb, db,Forms,Dialogs;

type

  { TFuncoes }

  TFuncoes = class
    class procedure AbrirDataSourcesForm(Form : TForm);
    class procedure FecharDataSourceForm(Form : TForm);
    class function ObterGenerator(NomeGenerator : string) : integer;
  end;

implementation

uses uDM1;

{ TFuncoes }

class procedure TFuncoes.AbrirDataSourcesForm(Form: TForm);
var i : integer;
begin
  for i := 0 to Form.ComponentCount - 1 do
  begin
    if Form.Components[i].ClassName = 'TDataSource' then
    begin
      TDataSource(Form.Components[i]).DataSet.Open;
      TDataSource(Form.Components[i]).Enabled:= True;
    end;
  end;

end;

class procedure TFuncoes.FecharDataSourceForm(Form: TForm);
var i : integer;
begin
  for i := 0 to Form.ComponentCount - 1 do
  begin
    if Form.Components[i].ClassName = 'TDataSource' then
    begin
      TDataSource(Form.Components[i]).DataSet.Close;
    end;
  end;
end;

class function TFuncoes.ObterGenerator(NomeGenerator: string): integer;
var qry : TSQLQuery = nil;
begin
  try
    try
      qry := TSQLQuery.Create(nil);
      qry.Transaction := DM1.SQLTConexao;
      qry.DataBase := DM1.CONEXAO;
      qry.SQL.Text:= 'select gen_id(' + NomeGenerator + ',1) from RDB$DATABASE';
      QRY.Open;
      Result := qry.FieldByName('GEN_ID').AsInteger;
    except
      on e : exception do
      begin
        ShowMessage('NÃO FOI POSSIVEL OBTER O GENERATOR' + #13 + e.Message);
        Result := null;
      end;
    end;
  finally
    qry.Free;
  end;
end;

end.

